package per.hongze.javatest.test;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import per.hongze.javatest.mappers.RoleMapper;
import per.hongze.javatest.mappers.UserMapper;
import per.hongze.javatest.pojo.User;
import per.hongze.javatest.utils.SqlSessionUtlis;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author YiDan
 * @version 1.0
 * @description: TODO
 * @date 2022/6/28 0:08
 */

public class MyBatisUseTes {

    /**
     * 不使用工具类使用mybatis完成添加功能
     *
     * @throws Exception
     */
    @Test
    public void testMyBatis() throws Exception {
        //加载配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //获取sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        //获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取mapper接口对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //获取当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());

        //测试功能
        User user = new User();
        user.setAccount("test");
        user.setPassword("123456");
        user.setAddtime(date);
        user.setEdittime(date);
        user.setRoleid(1);
        user.setStatus(1);
        int result = mapper.insertUser(user);
//        sqlSession.commit();//openSession传入true可以实现自动提交事务
        System.out.println("result:" + result);
    }

    /**
     * 使用工具类使用mybatis进行查询功能
     */
    @Test
    public void testGetAllUser() {
        SqlSession sqlSession = SqlSessionUtlis.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();
        for (User user : list) {
            System.out.println(user.getAddtime());
            System.out.println(user);
        }
    }

    /**
     * 测试mybatis一级缓存的使用
     */
    @Test
    public void testCacheOne() {
        SqlSession sqlSession = SqlSessionUtlis.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();
        for (User user : list) {
            System.out.println(user.getAddtime());
            System.out.println(user);
        }
        //手动清除一级缓存
        //sqlSession.clearCache();
        //测试一级缓存
        List<User> list2 = mapper.findAll();
        for (User user : list2) {
            System.out.println(user.getAddtime());
            System.out.println(user);
        }
    }

    /**
     * 测试mybatis二级缓存的使用
     */
    @Test
    public void testCacheTwo() throws Exception {
        //加载配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //获取sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        //获取sqlSession1
        SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
        //获取mapper接口对象
        RoleMapper mapper = sqlSession1.getMapper(RoleMapper.class);
        System.out.println(mapper.findAll());
        sqlSession1.close();
        //一级二级缓存期间使用了任意的增删改会被清空
        //获取sqlSession2
        SqlSession sqlSession2 = sqlSessionFactory.openSession(true);
        //获取mapper接口对象
        RoleMapper mapper2 = sqlSession2.getMapper(RoleMapper.class);
        System.out.println(mapper2.findAll());
        sqlSession2.close();
    }
}
